package com.topscore.uke.mvp.model;

import com.topscore.uke.entities.TeamPerfomenceSummaryData;
import com.topscore.uke.entities.TeamPerformanceRankData;
import com.topscore.uke.mvp.contractors.GetTeamPerformanceRankContractor;
import com.topscore.uke.network.BaseResponse;
import com.topscore.uke.network.request.GetTeamPerformanceRequest;
import com.topscore.uke.network.request.GetTeamRankRequest;
import com.topscore.uke.network.retrofit.HandleErrorSubscriber;

import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class GetTeamPerformanceRankModel implements GetTeamPerformanceRankContractor, GetTeamPerformanceRankContractor.Model {

    private CallBack mCallBack;

    public GetTeamPerformanceRankModel(CallBack callBack) {
        this.mCallBack = callBack;
    }

    @Override
    public void getData(GetTeamPerformanceRequest performanceRequest, GetTeamRankRequest rankRequest) {
        Observable observable = Observable.merge( mService.getTeamRankList(rankRequest),mService.getTeamPerformance(performanceRequest));
        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new HandleErrorSubscriber<BaseResponse<?>>(mCallBack) {
                    @Override
                    public void onSuccess(BaseResponse<?> response) {
                        if (response.getData() instanceof TeamPerfomenceSummaryData) {
                            mCallBack.onGetSummarySuccess((BaseResponse<TeamPerfomenceSummaryData>) response);
                        } else if (response.getData() instanceof TeamPerformanceRankData) {
                            mCallBack.onGetRankSuccess((BaseResponse<TeamPerformanceRankData>) response);
                        }
                    }
                });
    }

    @Override
    public void getRankData(GetTeamRankRequest rankRequest) {
        mService.getTeamRankList(rankRequest)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new HandleErrorSubscriber<BaseResponse<TeamPerformanceRankData>>(mCallBack) {
                    @Override
                    public void onSuccess(BaseResponse<TeamPerformanceRankData> response) {
                        mCallBack.onGetRankSuccess(response);
                    }
                });
    }

    @Override
    public void getPerformance(GetTeamPerformanceRequest performanceRequest) {
        mService.getTeamPerformance(performanceRequest)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new HandleErrorSubscriber<BaseResponse<TeamPerfomenceSummaryData>>(mCallBack) {
                    @Override
                    public void onSuccess(BaseResponse<TeamPerfomenceSummaryData> response) {
                        mCallBack.onGetSummarySuccess(response);
                    }
                });
    }
}
